"""
作者: 李聪
日期: 2022年 04月 14日
"""
import random
import sys
sys.setrecursionlimit(10000000)
m = 5
n = 5#定义数组大小
mx = [1, 0, 0, -1]
my = [0, -1, 1, 0]#代表向四个方向
color = [1, 2, 3, 4, 5]#颜色代号
array = [[random.randint(1, 5) for j in range(1, 6)]for i in range(1, 6)]
#array = [[1, 1, 1, 1, 2], [1, 1, 1, 2, 3], [1, 1, 3, 4,5], [1, 2, 2, 2, 2], [2, 1, 2, 3, 2]]#测试用例

def dfs(x , y, c):
    for i in range(0, 4):#向四个方向延伸
        tx = x + mx[i]
        ty = y + my[i]
        if (tx < 1 or tx > m) or (ty < 1 or ty > n):#排除边界外的点
            pass
        else:
            array[x - 1][y - 1] = c#改变颜色
            if array[tx - 1][ty - 1] == b:
                dfs(tx, ty, c)

for i in range(0, 5):
    print(array[i])
while True:
    flag = 0
    x1 = int(input())
    y1 = int(input())  # 输入点击方块
    c1 = int(input())  # 输入点击方块改变后的颜色
    b = array[x1 - 1][y1 - 1]  # 存下原来颜色
    dfs(x1, y1, c1)
    for i in range(0, 5):
        print(array[i])
    for j in range(0, m):
        for k in range(0, n):
            if array[j][k] != c1:
                flag = 1
    if flag == 0:
        break




